﻿// COPYRIGHT 2012, Justin Michalczak
//
// This file is licenced to the public under the Apache
// License, Version 2.0 (the "License"); you may not use
// this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace CopperVineSoftware.Stackr.Parser
{
    class CharacterClasses
    {
        public static bool AreWhitespace(IEnumerable<char> characters)
        {
            return characters.All(IsWhitespace);
        }

        public static bool IsWhitespace(char character)
        {
            UnicodeCategory category = char.GetUnicodeCategory(character);
            return (character == '\u0009' ||
                character == '\u000A' ||
                character == '\u000B' ||
                character == '\u000C' ||
                character == '\u000D' ||
                character == '\u0085' ||
                category == UnicodeCategory.SpaceSeparator ||
                category == UnicodeCategory.LineSeparator ||
                category == UnicodeCategory.ParagraphSeparator);
        }

        public static bool AreNameInitial(IEnumerable<char> characters)
        {
            return characters.All(IsNameInitial);
        }

        public static bool IsNameInitial(char character)
        {
            UnicodeCategory category = char.GetUnicodeCategory(character);
            return (category == UnicodeCategory.LowercaseLetter ||
                category == UnicodeCategory.UppercaseLetter ||
                category == UnicodeCategory.TitlecaseLetter);
        }

        public static bool AreNameNonInitial(IEnumerable<char> characters)
        {
            return characters.All(IsNameNonInitial);
        }

        public static bool IsNameNonInitial(char character)
        {
            if (IsNameInitial(character))
                return true;

            UnicodeCategory category = char.GetUnicodeCategory(character);
            return (category == UnicodeCategory.DecimalDigitNumber ||
                category == UnicodeCategory.LetterNumber);
        }
    }
}
